package com.enniswu.weitter.widget.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import java.util.List;

public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {

    private List<? extends Fragment> fragments;
    private List<String> titles;

    public FragmentPagerAdapter(FragmentManager fm, List<? extends Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    public void setTitles(List<String> titles) {
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return fragments.indexOf(object);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        if (titles == null || titles.size() < fragments.size())
            return super.getPageTitle(position);
        return titles.get(position);
    }
}
